﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class FuncNavKit
{
    public enum FuncNavType
    {
        NONE,
        TREASURE_COMPOSE,           // 合成饰品
        RUSH_RESET,                 // 试炼之地可重置
        RUSH_BOSS_LEFT_TIME,        // 试炼之地BOSS有次数
        MEMORY_SHOP_RESET_LEFT_TIME,    // 回忆圣坛有免费重置次数
        WANTED_BOSS_HAVE,           // 有悬赏BOSS可打
        WANTED_BOSS_REWARD,         // 有悬赏BOSS奖励可领取
        EXCEPTION_REWARD,           // 远征奖励可领取
        EXCEPTION_MISSION,          // 有远征可执行
        HELP_FRIEND,                // 有好友可协助
        WORLD_SEED_VALIABLE,        // 可以潜能解放
        HAVE_FORMATION_SOLT,        // 有可上阵的人
        HAVE_HELP_FORMATION_SOLT,   // 有可助阵的人
        ACTIVITY_COPY_LEFT_TIME,    // 活动副本有剩余次数
        MAKE_GEM,                   // 制作宝石
        COOK,                       // 制作料理
        UNION_CONTRIBUTE,           // 公会贡献
        UNION_REWARD_STAMINA,       // 公会领取体力
        UNION_REWARD,               // 公会领取奖励
        STAMINA_LEFT,               // 有体力
        ENERGY_LEFT,                // 有精力
        TIREDNESS_LEFT,             // 有疲劳值
    }

    public void OnRushReset()
    {
        Debug.Log("---------------------onRushReset");
        if (enableFuncs.Contains(FuncNavType.RUSH_RESET))
        {
            enableFuncs.Remove(FuncNavType.RUSH_RESET);
        }
    }

    public void OnRushEliteBossTimeCost(int curTime)
    {
        Debug.Log("----------------onRushEliteBossTimeCost: " + curTime);
        if (curTime <= 0)
        {
            if (enableFuncs.Contains(FuncNavType.RUSH_BOSS_LEFT_TIME))
            {
                enableFuncs.Remove(FuncNavType.RUSH_BOSS_LEFT_TIME);
            }
        }
        else
        {
            if (!enableFuncs.Contains(FuncNavType.RUSH_BOSS_LEFT_TIME))
            {
                enableFuncs.Add(FuncNavType.RUSH_BOSS_LEFT_TIME);
            }
        }

    }

    public void OnHelpFormationChange()
    {
        Debug.Log("-----------------onHelpFormationChange");
        if (StaticData.playerData.InstPlayerData.IsHaveEmptyHelpBattleCamp())
        {
            if (!enableFuncs.Contains(FuncNavType.HAVE_HELP_FORMATION_SOLT))
            {
                enableFuncs.Add(FuncNavType.HAVE_HELP_FORMATION_SOLT);
            }
        }
        else
        {
            if (enableFuncs.Contains(FuncNavType.HAVE_HELP_FORMATION_SOLT))
            {
                enableFuncs.Remove(FuncNavType.HAVE_HELP_FORMATION_SOLT);
            }
        }
    }

    public void OnFormationChange()
    {
        Debug.Log("----------------------onFormationChange");
        if (StaticData.playerData.InstPlayerData.IsHaveEmptyBattleCamp())
        {
            if (!enableFuncs.Contains(FuncNavType.HAVE_FORMATION_SOLT))
            {
                enableFuncs.Add(FuncNavType.HAVE_FORMATION_SOLT);
            }
        }
        else
        {
            if (enableFuncs.Contains(FuncNavType.HAVE_FORMATION_SOLT))
            {
                enableFuncs.Remove(FuncNavType.HAVE_FORMATION_SOLT);
            }
        }
    }

    public void OnCook()
    {
        Debug.Log("----------------OnCook");
        if (StaticData.playerData.IsHaveCanCook())
        {
            if (!enableFuncs.Contains(FuncNavType.COOK))
            {
                enableFuncs.Add(FuncNavType.COOK);
            }
        }
        else
        {
            if (enableFuncs.Contains(FuncNavType.COOK))
            {
                enableFuncs.Remove(FuncNavType.COOK);
            }
        }
    }

    public void OnGemMake()
    {
        Debug.Log("----------------OnGemMake");
        if (StaticData.playerData.IsHaveMineCanResolve())
        {
            if (!enableFuncs.Contains(FuncNavType.MAKE_GEM))
            {
                enableFuncs.Add(FuncNavType.MAKE_GEM);
            }
        }
        else
        {
            if (enableFuncs.Contains(FuncNavType.MAKE_GEM))
            {
                enableFuncs.Remove(FuncNavType.MAKE_GEM);
            }
        }
    }

    bool IsDailyCopyStateLevelLimit(List<ProtoBuf.Message.DailyCopyState> dailys)
    {
        bool isLimit = true;
        foreach (var daily in dailys)
        {
            if (StaticData.playerData.level >= daily.LevelLimit)
            {
                isLimit = false;
                break;
            }
        }
        return isLimit;
    }

    public void OnActivityCopyUpdate(ProtoBuf.Message.SCDailyCopyState msg)
    {
        bool isHaveCount = false;
        foreach (var item in msg.items)
        {
            if (item.isOpen > 0 && item.lastTime > 0 && !IsDailyCopyStateLevelLimit(item.dailyCopyState))
            {
                isHaveCount = true;
                break;
            }
        }

        if (isHaveCount)
        {
            if (!enableFuncs.Contains(FuncNavType.ACTIVITY_COPY_LEFT_TIME))
            {
                enableFuncs.Add(FuncNavType.ACTIVITY_COPY_LEFT_TIME);
            }
        }
        else
        {
            if (enableFuncs.Contains(FuncNavType.ACTIVITY_COPY_LEFT_TIME))
            {
                enableFuncs.Remove(FuncNavType.ACTIVITY_COPY_LEFT_TIME);
            }
        }

    }

    public void OnWorldSeedUpdate(int cur,int need)
    {
        if (cur < need)
        {
            if (enableFuncs.Contains(FuncNavType.WORLD_SEED_VALIABLE))
            {
                enableFuncs.Remove(FuncNavType.WORLD_SEED_VALIABLE);
            }
        }
        else
        {
            if (!enableFuncs.Contains(FuncNavType.WORLD_SEED_VALIABLE))
            {
                enableFuncs.Add(FuncNavType.WORLD_SEED_VALIABLE);
            }
        }
    }

    public void OnExceptionGetReward()
    {
        if (enableFuncs.Contains(FuncNavType.EXCEPTION_REWARD))
        {
            enableFuncs.Remove(FuncNavType.EXCEPTION_REWARD);
        }

    }

    public void OnExceptionAcceptCountChange(int cur, int max)
    {
        if (cur >= max)
        {
            if (enableFuncs.Contains(FuncNavType.EXCEPTION_MISSION))
            {
                enableFuncs.Remove(FuncNavType.EXCEPTION_MISSION);
            }
        }
        else
        {
            if (!enableFuncs.Contains(FuncNavType.EXCEPTION_MISSION))
            {
                enableFuncs.Add(FuncNavType.EXCEPTION_MISSION);
            }
        }

    }

    public void OnExceptionHelpFriend(int leftTime, int friendCount)
    {
        if (leftTime > 0 && friendCount > 0)
        {
            if (!enableFuncs.Contains(FuncNavType.HELP_FRIEND))
            {
                enableFuncs.Add(FuncNavType.HELP_FRIEND);
            }
        }
        else
        {
            if (enableFuncs.Contains(FuncNavType.HELP_FRIEND))
            {
                enableFuncs.Remove(FuncNavType.HELP_FRIEND);
            }
        }
    }

    public void OnMemoryShopReset(int leftResetTime)
    {
        if (leftResetTime > 0)
        {
            if (!enableFuncs.Contains(FuncNavType.MEMORY_SHOP_RESET_LEFT_TIME))
            {
                enableFuncs.Add(FuncNavType.MEMORY_SHOP_RESET_LEFT_TIME);
            }
        }
        else
        {
            if (enableFuncs.Contains(FuncNavType.MEMORY_SHOP_RESET_LEFT_TIME))
            {
                enableFuncs.Remove(FuncNavType.MEMORY_SHOP_RESET_LEFT_TIME);
            }
        }
    }

    public void OnUnionReceiveStamina()
    {
        if (enableFuncs.Contains(FuncNavType.UNION_REWARD_STAMINA))
        {
            enableFuncs.Remove(FuncNavType.UNION_REWARD_STAMINA);
        }
    }

    public void OnUnionContribute()
    {
        if (enableFuncs.Contains(FuncNavType.UNION_CONTRIBUTE))
        {
            enableFuncs.Remove(FuncNavType.UNION_CONTRIBUTE);
        }
    }

    public void OnUnionReceiveBox()
    {
        if (enableFuncs.Contains(FuncNavType.UNION_REWARD))
        {
            enableFuncs.Remove(FuncNavType.UNION_REWARD);
        }
    }

    public void OnWantedBossRefresh(int time, int count)
    {
        if (time > 0 && count > 0)
        {
            if (!enableFuncs.Contains(FuncNavType.WANTED_BOSS_HAVE))
            {
                enableFuncs.Add(FuncNavType.WANTED_BOSS_HAVE);
            }
        }
        else
        {
            if (enableFuncs.Contains(FuncNavType.WANTED_BOSS_HAVE))
            {
                enableFuncs.Remove(FuncNavType.WANTED_BOSS_HAVE);
            }
        }
    }

    public void OnWantedGetRewardRefresh(int rewardCount)
    {
        if (rewardCount > 0)
        {
            if (!enableFuncs.Contains(FuncNavType.WANTED_BOSS_HAVE))
            {
                enableFuncs.Add(FuncNavType.WANTED_BOSS_HAVE);
            }
        }
        else
        {
            if (enableFuncs.Contains(FuncNavType.WANTED_BOSS_HAVE))
            {
                enableFuncs.Remove(FuncNavType.WANTED_BOSS_HAVE);
            }
        }

    }

    public void OnTreasureCombineInfoRefresh(int canCombineCount)
    {
        if (canCombineCount > 0)
        {
            if (!enableFuncs.Contains(FuncNavType.TREASURE_COMPOSE))
            {
                enableFuncs.Add(FuncNavType.TREASURE_COMPOSE);
            }
        }
        else
        {
            if (enableFuncs.Contains(FuncNavType.TREASURE_COMPOSE))
            {
                enableFuncs.Remove(FuncNavType.TREASURE_COMPOSE);
            }
        }
    }

    public List<FuncNavType> enableFuncs = new List<FuncNavType>();

    public FuncNavKit()
    {
        enableFuncs = new List<FuncNavType>();
        StaticData.playerData.AddOnStaminaChanged(CheckStaminaLeft);
        StaticData.playerData.AddOnEnergyChanged(CheckEnergyLeft);
        StaticData.playerData.AddOnTirednessChanged(CheckTirednessLeft);
    }

    ~FuncNavKit()
    {
        StaticData.playerData.RemoveOnStaminaChange(CheckStaminaLeft);
        StaticData.playerData.RemoveOnEnergyChanged(CheckEnergyLeft);
        StaticData.playerData.RemoveOnTirednessChanged(CheckTirednessLeft);
    }

    public void RegistNetHandler()
    {
        NetAPI.RegistSocketHandler(ProtoBuf.Message.OpDefine.SCNavigationItem, FuncNavCallback);
        CheckStaminaLeft();
        CheckEnergyLeft();
        CheckTirednessLeft();
        CheckBattleCamp();
        CheckHelpBattleCamp();
    }
    public void UnRegistNetHandler()
    {
        NetAPI.UnregisterSocketHandler(ProtoBuf.Message.OpDefine.SCNavigationItem, FuncNavCallback);

    }

    public void OnLevelUp()
    {
        CheckStaminaLeft();
        CheckEnergyLeft();
        CheckTirednessLeft();
        CheckBattleCamp();
        CheckHelpBattleCamp();
    }

    public bool IsFuncNavHaveTip()
    {
        return enableFuncs.Count > 0;
    }

    public void FuncNavCallback(NetWork.Packets.Packet pack, bool bSuccess)
    {
        if (bSuccess)
        {
            FuncNavCallback(pack.kBody);
        }
    }

    public void FuncNavCallback(object obj)
    {
        ProtoBuf.Message.SCNavigationItem msg = obj as ProtoBuf.Message.SCNavigationItem;
        Debug.Log("FuncNavCallback               " + msg.navItems.Count);
        List<FuncNavType> cache = new List<FuncNavType>();
        foreach (var func in enableFuncs)
        {
            if (IsFuncTypeLocalDeal(func))
            {
                cache.Add(func);
            }
        }
        enableFuncs.Clear();
        enableFuncs.AddRange(cache);
        foreach (var item in msg.navItems)
        {
            enableFuncs.Add((FuncNavType)item.type);
        }
    }


    bool IsItemNeedTip(ProtoBuf.Message.NavigationItem item)
    {
        if (item.value == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    bool IsFuncTypeLocalDeal(FuncNavType type)
    {
        switch (type)
        {
            case FuncNavType.ENERGY_LEFT:
            case FuncNavType.STAMINA_LEFT:
            case FuncNavType.TIREDNESS_LEFT:
            case FuncNavType.HAVE_FORMATION_SOLT:
            case FuncNavType.HAVE_HELP_FORMATION_SOLT:
                return true;
            default:
                return false;
        }
    }

    void CheckStaminaLeft()
    {
        if (!CheckFunc(FuncNavType.STAMINA_LEFT) && enableFuncs.Contains(FuncNavType.STAMINA_LEFT))
        {
            enableFuncs.Remove(FuncNavType.STAMINA_LEFT);
        }
        else if (CheckFunc(FuncNavType.STAMINA_LEFT) && !enableFuncs.Contains(FuncNavType.STAMINA_LEFT))
        {
            enableFuncs.Add(FuncNavType.STAMINA_LEFT);
        }
    }

    void CheckEnergyLeft()
    {
        if (!CheckFunc(FuncNavType.ENERGY_LEFT) && enableFuncs.Contains(FuncNavType.ENERGY_LEFT))
        {
            enableFuncs.Remove(FuncNavType.ENERGY_LEFT);
        }
        else if(CheckFunc(FuncNavType.ENERGY_LEFT) && !enableFuncs.Contains(FuncNavType.ENERGY_LEFT))
        {
            enableFuncs.Add(FuncNavType.ENERGY_LEFT);
        }
    }

    void CheckTirednessLeft()
    {
        if (!CheckFunc(FuncNavType.TIREDNESS_LEFT) && enableFuncs.Contains(FuncNavType.TIREDNESS_LEFT))
        {
            enableFuncs.Remove(FuncNavType.TIREDNESS_LEFT);
        }
        else if (CheckFunc(FuncNavType.TIREDNESS_LEFT) && !enableFuncs.Contains(FuncNavType.TIREDNESS_LEFT))
        {
            enableFuncs.Add(FuncNavType.TIREDNESS_LEFT);
        }
    }

    void CheckBattleCamp()
    {
        bool ret = CheckFunc(FuncNavType.HAVE_FORMATION_SOLT);
        if (!ret && enableFuncs.Contains(FuncNavType.HAVE_FORMATION_SOLT))
        {
            enableFuncs.Remove(FuncNavType.HAVE_FORMATION_SOLT);
        }
        else if (ret && !enableFuncs.Contains(FuncNavType.HAVE_FORMATION_SOLT))
        {
            enableFuncs.Add(FuncNavType.HAVE_FORMATION_SOLT);
        }
    }

    void CheckHelpBattleCamp()
    {
        bool ret = CheckFunc(FuncNavType.HAVE_HELP_FORMATION_SOLT);
        if (!ret && enableFuncs.Contains(FuncNavType.HAVE_HELP_FORMATION_SOLT))
        {
            enableFuncs.Remove(FuncNavType.HAVE_HELP_FORMATION_SOLT);
        }
        else if (ret && !enableFuncs.Contains(FuncNavType.HAVE_HELP_FORMATION_SOLT))
        {
            enableFuncs.Add(FuncNavType.HAVE_HELP_FORMATION_SOLT);
        }
    }

    bool CheckFunc(FuncNavType type)
    {
        if (IsFuncTypeLocalDeal(type))
        {
            switch (type) {
                case FuncNavType.ENERGY_LEFT:
                    if (StaticData.playerData.Energy >= Dict.Blo.DictConfigBlo.GetFuncNavEnergyLimit() &&
                        StaticData.playerData.level >= Dict.Blo.DictConfigBlo.GetRankPVPLimitLevel())
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case FuncNavType.STAMINA_LEFT:
                    if (StaticData.playerData.Stamina >= Dict.Blo.DictConfigBlo.GetFuncNavStaminaLimit())
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case FuncNavType.TIREDNESS_LEFT:
                    if (StaticData.playerData.Tiredness >= Dict.Blo.DictConfigBlo.GetFuncNavTiredlessLimit() &&
                        StaticData.playerData.level >= Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.OutdoorLevel)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case FuncNavType.HAVE_FORMATION_SOLT:
                    if (StaticData.playerData.InstPlayerData.IsHaveEmptyBattleCamp())
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case FuncNavType.HAVE_HELP_FORMATION_SOLT:
                    if (StaticData.playerData.InstPlayerData.IsHaveEmptyHelpBattleCamp())
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
            }

        }
        return false;
    }


}